﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Web.Mvc;

namespace Cipher.Web.MVC {
    public sealed class ControllerRefleactor {
        //public static IList<AuthorizeModel> GetAllRules() {
        //    //List<AuthorizeModel> authList = new List<AuthorizeModel>();

        //    Assembly assembly = Assembly.GetCallingAssembly();
            
        //    Type controllerType = typeof(Controller);

        //    Type[] types = assembly.GetTypes();

        //    foreach (Type type in types) {
        //        if (type.IsSubclassOf(controllerType)){
        //            authList.AddRange(GetActions(type));
        //        }
        //    }

        //    return authList;
        //}

        //private static IList<AuthorizeModel> GetActions(Type controllerType) {
        //    List<AuthorizeModel> authLists = new List<AuthorizeModel>();

        //    MethodInfo[] methods = controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
        //    foreach(MethodInfo method in methods) {
        //        object[] objs = method.GetCustomAttributes(typeof(AuthorizeDefineAttribute), false);

        //        if(objs.Length > 0) {
        //            AuthorizeDefineAttribute attr = objs[0] as AuthorizeDefineAttribute;

        //            if(!string.IsNullOrEmpty(attr.Rule) && !string.IsNullOrEmpty(attr.RuleName)) {
        //                AuthorizeModel model = new AuthorizeModel();
        //                model.ControllerName = controllerType.Name;
        //                model.ActionName = method.Name;

        //                model.Rule = attr.Rule;
        //                model.RuleName = attr.RuleName;
        //                model.Description = attr.Description;

        //                authLists.Add(model);
        //            }
        //        }
        //    }

        //    return authLists;
        //}
    }
}